<template>
  <el-card class="box-card">

    <template #header>
      <div class="card-header">
        <span>{{ labelName || '类别' }}</span>
      </div>
    </template>

    <div class="text item" v-for="item in labelList" :key="item._id" @click="handleRouter(item.labelName)"
      v-if="!showArticleList">
      <el-icon :size="20" class="itemLight">
        <Opportunity />
      </el-icon> &emsp;
      <div class="label">
        <span> {{ item.labelName }}</span>
        <span>文章数 ({{ item.articleCount }})</span>
      </div>
    </div>
    <div class="ArticleList" v-if="showArticleList">
      <ArticleList :article-where="{ articleType: labelName }" ref="ArticleListRef"></ArticleList>

    </div>
  </el-card>
</template>

<script lang="ts" setup>
import { ref, defineAsyncComponent } from 'vue'
import { Opportunity } from '@element-plus/icons-vue'
import { getArticleClassify } from '@/utils/http/articleHttp'
// 导入文章列表
const ArticleList = defineAsyncComponent(() => import('@/components/article/ArticleList.vue'))

type labelList = { labelName: string, articleCount: string, _id: string }
const labelList = ref<labelList[]>([])

getArticleClassify().then(res => {
  labelList.value = res.data.info.filter((item: labelList) => item.labelName != '草稿')
})
let ArticleListRef = ref<InstanceType<typeof ArticleList>>()
let showArticleList = ref<boolean>(false)
let labelName = ref<string>('')
const handleRouter = (label: string) => {
  labelName.value = label
  showArticleList.value = true
  setTimeout(() => {
    ArticleListRef.value && ArticleListRef.value.getArticleList()
  }, 200)
}
</script>


<style lang="scss" scoped>
.ArticleList {
  background: #f5f5f5;
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 16px;
  color: #777;
  font-weight: 900;
}

.box-card {
  padding: 20px !important;

}

.item {
  display: flex;
  padding-top: 18px;
  color: #777;
  font-size: 16px;
  align-items: center;
  padding-bottom: 5px;
  border-bottom: 1px dashed #ddd;
  cursor: pointer;

  .label {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;

    span:nth-child(2) {
      font-size: 12px;
      color: #888;
      margin-right: 40px;
    }
  }

  &:hover .itemLight {
    color: red;
  }
}

.box-card {
  max-width: 1200px;
  margin: auto;
  padding: 40px 50px;
  min-height: 500px;
  margin-top: -50px;
}

.itemLight {
  color: #49b1f5;
}
</style>